import 'dart:async';

import 'package:flu_framework/get_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      decoration: BoxDecoration(
          color: Colors.white
      ),
      child: Stack(
        children: [
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: Image.asset("images/icon_splash_top.png"),
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Image.asset("images/icon_splash_bottom.png"),
          ),
          Positioned(
              top: 10,
              right: 10,
              child: CountDownWidget(
                onCountDownFinishCallBack: (bool value) {
                  if(value) {
                    Get.offNamed(PageName.PAGE_MAIN);
                  }
                },
              ))
        ],
      ),
    );
  }
}

class CountDownWidget extends StatefulWidget {

  final onCountDownFinishCallBack;

  const CountDownWidget({Key? key,required this.onCountDownFinishCallBack}) : super(key: key);

  @override
  State<CountDownWidget> createState() => _CountDownWidgetState();
}

class _CountDownWidgetState extends State<CountDownWidget> {
  var _seconds = 2;
  late Timer _timer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _startTimer();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 50,
        height: 50,
        child: ElevatedButton(
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.all(Colors.grey),
            foregroundColor: MaterialStateProperty.all(Colors.white),
            padding: MaterialStateProperty.all(EdgeInsets.all(2)),
            elevation: MaterialStateProperty.all(20),
            shape: MaterialStateProperty.all(
                RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(40))
            ),
          ),
          child: Text(
            "跳过${_seconds}",
            style: TextStyle(
                fontSize: 13,
                color: Colors.white
            ),
          ),
          onPressed: () async{
            Get.offNamed(PageName.PAGE_MAIN);
          },
        )
    );
  }

  void _startTimer() {
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {});
      if(_seconds <= 1) {
        widget.onCountDownFinishCallBack(true);
        _cancelTimer();
        return;
      }
      _seconds--;

    });
  }

  void _cancelTimer() {
    _timer.cancel();
  }


}
